package com.example.first;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class MapTask implements Runnable{
    private  static final String TAG="MapTask";
    private Handler handler;
    public void setHandler(Handler handler){
        this.handler=handler;
    }
    @Override
    public void run() {
        //获取网络数据
        //时间延时
        Log.i(TAG,"run:run().....");
        try {
            Thread.sleep(3000);
        }catch(InterruptedException e){
            e.printStackTrace();
        }
        //获取页面数据
        List<HashMap<String,String>> rateList = new ArrayList<HashMap<String,String>>();
        try{
            String url= "https://www.waihui580.com/";
            Document doc= Jsoup.connect(url).get();
            Log.i(TAG,"run:"+doc.title());
            Elements tables=doc.getElementsByTag("table");
           /* String html= "https://job.swufe.edu.cn/jyhd.htm";
            Document doc1= Jsoup.parse(html);
            Elements elements=doc1.getElementsByClass("noticeLi");
            Log.i(TAG,"elements"+elements.size());
            String abs=elements.select("p").text();
            Log.i(TAG,"elements"+abs);*/
            Element table6=tables.get(0);
            Elements tds=table6.getElementsByTag("td");
            for (int i = 0; i < tds.size() - 6; i += 7) {
                Element td1 = tds.get(i);
                Element td2 = tds.get(i + 6);
                String str1 = td1.text();
                String val = td2.text();
                Log.i(TAG, "run:" + str1 + "==>" + val);
                HashMap<String,String> map=new HashMap<String,String>();
                map.put("cname",str1);
                map.put("cval",val);
                rateList.add(map);
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        //返回消息给主线程
        Message msg=handler.obtainMessage(7,rateList);
        handler.sendMessage(msg);
        Log.i(TAG,"RUN:sendMessage ok!");
    }
}
